package com.qf.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfig {

    @Value("${order.shopOrderQueueName}")
    private String shopOrderQueueName;

    @Value("${order.shopOrderExchangeName}")
    private String shopOrderExchangeName;

    @Value("${order.shopOrderRouing}")
    private String shopOrderRouing;

    // 死信交换机
    @Bean
    public TopicExchange deadExchange() {
        return new TopicExchange("shop-order-dead-exchange", true, false);
    }

    @Bean
    public Queue deadQueue() {
        return new Queue("shop-order-dead-queue", true, false, false);
    }

    @Bean
    public Binding deadQueueToDeadExchange() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("shop-order-dead.*");
    }

    @Bean
    public Queue shopOrderQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-message-ttl", 1000 * 60); // 超时时间是1m
        args.put("x-dead-letter-routing-key", "shop-order-dead.error"); // 设置死信的路由键
        args.put("x-dead-letter-exchange", "shop-order-dead-exchange"); // 设置死信的交换机
        return new Queue(shopOrderQueueName, true, false, false,args);
    }

    @Bean
    public TopicExchange shopOrderExchange() {
        return new TopicExchange(shopOrderExchangeName, true, false);
    }

    @Bean
    public Binding shopOrderQueueBindiShopOrderExchange() {
        return BindingBuilder.bind(shopOrderQueue()).to(shopOrderExchange()).with(shopOrderRouing);
    }

}
